'''

银行家算法

'''
import random as rm

m = eval(input("请输入所要创建进程的个数:"))
n = eval(input("请输入系统资源种类数："))

#资源初始化
max1 = [[0 for j in range(n)]for i in range(m)]
allocation = [[0 for j in range(n)] for i in range(m)]
need = [[0 for j in range(n)] for i in range(m)]
request = [[0 for j in range(m)] for i in range(n)]
finish = [False for i in range(m)]
work = [0 for i in range(n)]
st1 = [0 for i in range(m)]

#使用空格符分割资源数目
available = input("请输入系统初始n类资源数目：").split()

#使用空格符分割资源数目
for i in range(n):
    available[i] = int(available[i])
print("请依次输入m个进程的所需最大资源数目：")
 
for i in range(m):
    max1[i] = input().split()
    for j in range(n):
        max1[i][j] = int(max1[i][j])
 
print("请依次输入m个进程已分配相应资源数目：")
for i in range(m):
    allocation[i] = input().split()
    for j in range(n):
        allocation[i][j] = int(allocation[i][j])
        need[i][j] = max1[i][j] - allocation[i][j]
 
print(available)
 
print("请输入所要请求资源的进程代码")
re_member = input()
re_member = int(re_member)
 
list1 = input("请输入进程相应进程的n维请求向量：").split()
for i in range(n):
    list1[i] = int(list1[i])
for i in range(n):
    if list1[i] > available[i]:
        print("系统现无足够资源，该进程须等待")
        break
 
for i in range(n):
    #分配进程号为re_member资源
    available[i] = available[i]-list1[i]
    allocation[re_member][i] = allocation[re_member][i]+list1[i]
    need[re_member][i] = need[re_member][i]-list1[i]
work = available
 
#安全性算法检查
 
print(work)
j = 0
si = 0
while j < m:
    if finish[j] is False and need[j] <= work:
        work = [work[i] + allocation[j][i] for i in range(n)]
        finish[j] = True
        st1[si] = j
        si = si + 1
        j = 0
    else:
        j = j + 1
 
for i in range(m):
    if finish[i] is False:
        st1 = [0 for i in range(m)]
        si = 0
        work = available
        break
    else:
        continue
 
 
flag = True
for i in range(m):
    if finish[i] is False:
        flag = False
        break
 
if flag is True:
    print("存在安全系列:")
    print(st1)
else:
    print("系统处于不安全状态")
